import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 * Date: 2022-10-31
 * Time: 21:57
 */
public class Test1 {
    public static void main(String[] args) {
        int[] array = {23,67,2,3,5};
        megerSort(array);
        System.out.println(Arrays.toString(array));
    }

    private static void megerSort(int[] array) {
        megerChild(array,0,array.length-1);
    }

    private static void megerChild(int[] array, int left, int right) {
        if(left == right){
            return;
        }
        int mid = (left+right)/2;
        megerChild(array,left,mid);
        megerChild(array,mid+1,right);
        meger(array,left,mid,right);
    }

    private static void meger(int[] array, int left, int mid, int right) {
        int s1 = left;
        int e1= mid;
        int s2 = mid+1;
        int e2 = right;
        int  k = 0;
        int[] tmp = new int[right-left+1];
        while (s1 <= e1 && s2 <= e2){
            if(array[s1] < array[s2]){
                tmp[k++] = array[s1++];
            }else{
                tmp[k++] = array[s2++];
            }
        }
        while (s1 <= e1){
            tmp[k++] = array[s1++];
        }
        while (s2 <= e2){
            tmp[k++] = array[s2++];           ;
        }
        for (int i = 0; i < k; i++) {
            array[i+left] = tmp[i];
        }
    }
}
